package com.atguigu.dga.governance.assessor.spec;

import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor  extends Assessor {

    Pattern odsPattern=Pattern.compile("^ods_\\w+_(inc|full)$");
    Pattern dimPattern=Pattern.compile("^dim_\\w+_(zip|full)$");
    Pattern dwdPattern=Pattern.compile("^dwd_\\w+_\\w+_(inc|full)$");
    Pattern dwsPattern=Pattern.compile("^dws_\\w+_\\w+_\\w+_(1d|nd|td)$");

    Pattern adsPattern=Pattern.compile("^ads_\\w+");

    Pattern dmPattern=Pattern.compile("^dm_\\w+");

    @Override
    protected void assessProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {

        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();
        String tableName = assessParam.getTableMetaInfo().getTableName();
        Matcher matcher = null;
        if(dwLevel.equals("ODS")) {
            matcher = odsPattern.matcher(tableName);
        }else if (dwLevel.equals("DWD")) {
            matcher = dwdPattern.matcher(tableName);
        }else if (dwLevel.equals("DIM")) {
            matcher = dimPattern.matcher(tableName);
        }else if (dwLevel.equals("DWS")) {
            matcher = dwsPattern.matcher(tableName);
        }else if (dwLevel.equals("ADS")) {
            matcher = adsPattern.matcher(tableName);
        }else if (dwLevel.equals("DM")) {
            matcher = dmPattern.matcher(tableName);
        }
        if(dwLevel.equals("OTHER")){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
        } else if (!matcher.matches()) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表名不符合规范");
        }

    }


    public static void main(String[] args) {

        String testEmail="!afadf@atguigu.com";

        Pattern emailPattern = Pattern.compile("^\\w+@\\w{1,10}\\.(com|cn|org|io)$");

        Matcher matcher = emailPattern.matcher(testEmail);

        if(matcher.matches()){
            System.out.println(" 符合 ");

        }else{
            System.out.println(" 不符合 ");
        }


    }
}
